clear all;
close all;

%read data
%Y Target Variable
Y = csvread('Data\DataApp1\3cities\Y.csv');

%X rawdata training data set
X = csvread('Data\DataApp1\3cities\X.csv');

%feature scaling to avoid overflow
[X, mu, st] = featureScaling(X);

%get data set size
M = size(X,1);
%K-folder parameter
K = 10;
%idexers for perform crossvalidation
indexers = crossvalind('Kfold',M,K);

%parameter to analyse
MaxItS = [200, 400, 600];
Lambdas = [0, 0.01, 0.02, 0.04, 0.08, 0.16 , 0.32, 0.64, 1.28, 2.56, 5.12, 10];
Alphas = [0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1];

%output description
CVLines = length(MaxItS) * length(Alphas) * length(Lambdas);
cvModelCount = 1;
cvModelDescription = zeros(CVLines, 4);

%loop through the max number of iterations
for maxItID = 1:length(MaxItS);
    %loop through alphas values
    for alphaID = 1: length(Alphas)
        %loop through lambdas values
        for lambdaID = 1: length(Lambdas)
            
            %assign parameters
            maxit = MaxItS(maxItID);
            alpha = Alphas(alphaID);
            lambda =  Lambdas(lambdaID);
            
            %intace for store the erros
            error = zeros(K,1);
            
            for i = 1:K
                %get test indexers
                testInd = (indexers == i);
                %get training indexes
                trainInd = ~testInd;
                
                %train the model
                [Poptim, PCostO, Woptim, WCostO, Coptim] = approach1(Y(trainInd,:), X(trainInd,:), maxit, alpha, lambda);
                
                %Evaluate the model 
                [H, cost] = modelPrediction(Y(testInd,:), X(testInd,:), Poptim, Woptim);
                
                %store the error
                error(i,1) = cost;
            end;
            
            %save the outcome of each model
            cvModelDescription(cvModelCount,:) = [maxit, alpha, lambda, (sum(error)/K)];
            cvModelCount = cvModelCount + 1;
            
            disp('models tested (maxit,alpha,lambda,error): ');
            disp(((cvModelCount/252) * 100));
            disp(cvModelDescription(cvModelCount - 1,:));
            
        end;
    end;
end;

save cvModelDescription;
